﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.IO;
using HousingInfo.Library;
namespace WebHousingInfo
{
    public partial class XemTinTheoQuanHuyen : System.Web.UI.Page
    {
        private static string modehousetype = null, modecate = null, modeareas = null, modeareadetail = null, modesave = null, modesavecomplete = null;
        private static NewsSaveBO newsaveBO;
        private static int savepage = 0, currentpage, numpage;
        protected void Page_Load(object sender, EventArgs e)
        {
            modehousetype = Request.QueryString["typeid"];
            modeareas = Request.QueryString["areaid"];
            modeareadetail = Request.QueryString["areadetailid"];
            modecate = Request.QueryString["cateid"];
            modesave = Request.QueryString["save"];
            modesavecomplete = Request.QueryString["action"];
            string linkpaging = "";                
            if (modehousetype != null)
            {
                linkpaging = "XemTinTheoQuanHuyen.aspx?typeid=" + modehousetype;
                string name = HousingTypeBO.GetHousingTypeBO(Convert.ToInt32(modehousetype)).HousingTypeName;
                lbtb.Text = name;                
            }
            else if (modeareas != null)
            {
                linkpaging = "XemTinTheoQuanHuyen.aspx?areaid=" + modeareas;
                string name = AreaBO.GetAreaBO(Convert.ToInt32(modeareas)).AreaName;
                lbtb.Text = name;
            }
            else if (modeareadetail != null)
            {
                linkpaging = "XemTinTheoQuanHuyen.aspx?areadetailid=" + modeareadetail;
                string name = AreaDetailBO.GetName_ByID(Convert.ToInt32(modeareadetail)).AreaDetailName;
                lbtb.Text = name;
            }
            else if (modecate != null)
            {
                linkpaging = "XemTinTheoQuanHuyen.aspx?cateid=" + modecate;
                string name = CategoryBO.GetCategoryBO(Convert.ToInt32(modecate)).CategoryName;
                lbtb.Text = name;
            }
            //luu tin
            if (modesavecomplete == "complete")
            {
                ScriptManager.RegisterStartupScript(Page, Page.GetType(), "Success", "alert('Đã lưu tin !')", true);
            }
            if (modesave != null)
            {
                if (Session["UserName"] == null)
                    Response.Redirect("DangNhap.aspx");
                if (NewsSaveBOList.CheckVip_Exits(Convert.ToInt32(modesave)) == false)
                {
                    newsaveBO = NewsSaveBO.NewNewsSaveBO();
                    newsaveBO.NewsHouseID = Convert.ToInt32(modesave);
                    newsaveBO.UserName = Session["UserName"].ToString();
                    try
                    {
                        newsaveBO.Save();
                        Response.Redirect("XemTinTheoQuanHuyen.aspx?" + Getlink() + "&action=complete" + getlinkpaging());
                    }
                    catch { }
                }
                else ScriptManager.RegisterStartupScript(Page, Page.GetType(), "Success", "alert('Đã lưu tin này !')", true);
            }

            numpage = 10;
            string current = null;
            current = Request.QueryString["page"];
            if (current != null)
                currentpage = int.Parse(current);
            else currentpage = 1;
            int totalrow = gettotalpage();
            int totalpage = totalrow / numpage;
            if (totalrow % numpage != 0)
                totalpage += 1;
            lblpaging.Text = "";
            if (currentpage > 1)
            {
                lblpaging.Text += "<ul><li><a href=\"" + linkpaging + "&page=1\">◄◄</a></li>";
                lblpaging.Text += "<li><a href=\"" + linkpaging + "&page=" + (currentpage - 1).ToString() + "\">◄</a></li>";
            }
            else
            {
                lblpaging.Text += "<ul><li><a>◄◄</a></li>";
                lblpaging.Text += "<li><a>◄</a></li>";
            }
            int custom = 5;
            if (currentpage > custom)
            {
                if (currentpage > savepage)
                {
                    if (currentpage + custom <= totalpage)
                    {
                        for (int i = currentpage; i <= currentpage + custom; i++)
                        {
                            if (i == currentpage)  // Nếu $_GET['p']  == trang hiện tại (current)
                            {
                                lblpaging.Text += "<li class=\"select\"><a>" + i.ToString() + "</a></li>";
                            }
                            else  // Trang khác
                            {
                                lblpaging.Text += "<li><a href=\"" + linkpaging + "&page=" + i.ToString() + "\"><strong>" + i.ToString() + "</strong></a></li>";
                            }

                        }// end for
                    }
                    else
                    {
                        for (int i = totalpage - custom; i <= totalpage; i++)
                        {
                            if (i == currentpage)  // Nếu $_GET['p']  == trang hiện tại (current)
                            {
                                lblpaging.Text += "<li class=\"select\"><a>" + i.ToString() + "</a></li>";
                            }
                            else  // Trang khác
                            {
                                lblpaging.Text += "<li><a href=\"" + linkpaging + "&page=" + i.ToString() + "\"><strong>" + i.ToString() + "</strong></a></li>";
                            }

                        }// end for
                    }
                }
                else
                {
                    if (currentpage - custom >= 1)
                    {
                        for (int i = currentpage - custom; i <= currentpage; i++)
                        {
                            if (i == currentpage)  // Nếu $_GET['p']  == trang hiện tại (current)
                            {
                                lblpaging.Text += "<li class=\"select\"><a>" + i.ToString() + "</a></li>";
                            }
                            else  // Trang khác
                            {
                                lblpaging.Text += "<li><a href=\"" + linkpaging + "&page=" + i.ToString() + "\"><strong>" + i.ToString() + "</strong></a></li>";
                            }

                        }// end for
                    }
                    else
                    {
                        for (int i = 1; i <= currentpage; i++)
                        {
                            if (i == currentpage)  // Nếu $_GET['p']  == trang hiện tại (current)
                            {
                                lblpaging.Text += "<li class=\"select\"><a>" + i.ToString() + "</a></li>";
                            }
                            else  // Trang khác
                            {
                                lblpaging.Text += "<li><a href=\"" + linkpaging + "&page=" + i.ToString() + "\"><strong>" + i.ToString() + "</strong></a></li>";
                            }

                        }// end for
                    }
                }
            }
            else
            {
                if (custom > totalpage)
                    custom = totalpage;
                for (int i = 1; i <= custom; i++)
                {
                    if (i == currentpage)
                    {
                        lblpaging.Text += "<li class=\"select\"><a>" + i.ToString() + "</a></li>";
                    }
                    else  // Trang khác
                    {
                        lblpaging.Text += "<li><a href=\"" + linkpaging + "&page=" + i.ToString() + "\"><strong>" + i.ToString() + "</strong></a></li>";
                    }

                }// end for
            }

            if (currentpage < totalpage)
            {
                lblpaging.Text += "<li><a href=\"" + linkpaging + "&page=" + (currentpage + 1).ToString() + "\">►</a></li>";
                lblpaging.Text += "<li><a href=\"" + linkpaging + "&page=" + totalpage.ToString() + "\">►►</a></li></ul>";
            }
            else
            {
                lblpaging.Text += "<li><a>►</a></li>";
                lblpaging.Text += "<li><a>►►</a></li></ul>";
            }
            savepage = currentpage;
        }

        public bool checkurlimage(string imagestring)
        {
            var pathFileImage = Server.MapPath("TotalUpload/ImagesTotal/" + imagestring);
            if (File.Exists(pathFileImage))
            {
                return true;
            }
            else return false;
        }

        public string Getlink()
        {  
            string link = "";
            if (modeareas != null)
                link = "areaid=" + modeareas;
            else if (modeareadetail != null)
                link = "areadetailid=" + modeareadetail;
            else if (modecate != null)
                link = "cateid=" + modecate;
            else
                link = "typeid=" + modehousetype;
            return link;
        }

        public NewsHouseBOList getdatasource()
        {
            NewsHouseBOList newsbolist;
            if (modehousetype != null)
            {
                newsbolist = NewsHouseBOList.GetNewsHouse_ByHoutype_ByArea(Convert.ToInt32(modehousetype), "loaidat", currentpage, numpage);
                return newsbolist;
            }
            else if (modeareas != null)
            {
                newsbolist = NewsHouseBOList.GetNewsHouse_ByHoutype_ByArea(Convert.ToInt32(modeareas), "quanhuyen", currentpage, numpage);
                return newsbolist;
            }
            else if (modeareadetail != null)
            {
                newsbolist = NewsHouseBOList.GetNewsHouse_ByHoutype_ByArea(Convert.ToInt32(modeareadetail), "duong", currentpage, numpage);
                return newsbolist;
            }
            else if (modecate != null)
            {
                newsbolist = NewsHouseBOList.GetNewsHouse_ByHoutype_ByArea(Convert.ToInt32(modecate), "danhmuc", currentpage, numpage);
                return newsbolist;
            }
            else return null;
        }

        public int gettotalpage()
        {
            int total = 0;
            if (modehousetype != null)
            {
                total = NewsHouseBOList.GetNewsHouse_ByHoutype_ByArea(Convert.ToInt32(modehousetype), "loaidat", -1, -1).Count;
            }
            else if (modeareas != null)
            {
                total = NewsHouseBOList.GetNewsHouse_ByHoutype_ByArea(Convert.ToInt32(modeareas), "quanhuyen", -1, -1).Count;
            }
            else if (modeareadetail != null)
            {
                total = NewsHouseBOList.GetNewsHouse_ByHoutype_ByArea(Convert.ToInt32(modeareadetail), "duong", -1, -1).Count;
            }
            else if (modecate != null)
            {
                total = NewsHouseBOList.GetNewsHouse_ByHoutype_ByArea(Convert.ToInt32(modecate), "danhmuc", -1, -1).Count;
            }
            return total;
        }

        public string getlinkpaging()
        {
            string link = null;
            link = Request.QueryString["page"];
            if (link != null)
                return "&page=" + link;
            else return "";
        }
    }
}